import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import User from '../components/user/User'
import Role from '../components/role/Role'
import UserPermission from '../components/role/UserPermission'
import Goods from '../components/goods/Goods'
import GoodsType from '../components/goods/GoodsType'
import Type from '../components/goods/Type'
import qs from 'qs'

Vue.prototype.$Qs = qs;
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/userList',
      component: User
    }, {
      path: '/roleList',
      component: Role
    }, {
      path: '/powerList',
      component: UserPermission
    }, {
      path: '/goodsList',
      component: Goods
    }, {
      path: '/goodsType',
      component: GoodsType
    }, {
      path: '/typeList',
      component: Type
    }]
  }
]

const router = new VueRouter({
  routes
})

//拦截路由守卫，判断访问请求路由是否携带token，未携带，跳转登录页
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const token = window.sessionStorage.getItem('token')
  if (!token) {//如果后端token过期了，怎么处理？？？
    return next('/login')
  }
  return next()
})

export default router
